package com.application.chiettinhdienluc.rsclient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

public class HttpHelper {
   
    public static String request(HttpResponse response, boolean useGZIP){
        String result = "";
        try{
            InputStream in = response.getEntity().getContent();
            
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
            	in = new GZIPInputStream(in);
            }
            
//            if(useGZIP)
//            {
//            	in = new GZIPInputStream(in);// Decode Gzip
//            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null){
                str.append(line + "\n");
            }
            in.close();
            result = str.toString();
        }
        catch(OutOfMemoryError e) {        	
			e.printStackTrace();
		}
        catch(Exception ex){
        	String error = ex.toString();
            result = "Error";
        }
        return result;
    }
} 